/*   
 * Copyright (c) 2016-2020 canaanQd. All Rights Reserved.   
 *   
 * This software is the confidential and proprietary information of   
 * canaanQd. You shall not disclose such Confidential Information   
 * and shall use it only in accordance with the terms of the agreements   
 * you entered into with canaanQd.   
 *   
 */ 
package com.org.cloud.lg.controller;
import com.org.cloud.lg.api.UserService;
import com.org.cloud.lg.dto.UserDto;
import com.org.cloud.lg.model.BaseController;
import com.org.cloud.lg.model.PageDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
public class UserController extends BaseController<UserService> {

	@Autowired
	BCryptPasswordEncoder bCryptPasswordEncoder;

	public void listen(String input) {
		System.out.println("=======================");
		System.out.println(input);
	}

	@GetMapping("info")
	public UserDto info(){
		return service.info(getUserId());
	}

	@GetMapping("getById")
	public UserDto getById(String id){
		return service.getById(id);
	}

	@PostMapping("page")
	public PageDto<UserDto> page(@RequestBody  PageDto<UserDto> userDto){
		return service.listPage(userDto);
	}


	@PostMapping("save")
	public boolean save(@RequestBody  UserDto userDto){
		userDto.setPassword(bCryptPasswordEncoder.encode(userDto.getPassword()));
		return service.save(userDto);
	}

	@PostMapping("update")
	public boolean update(@RequestBody  UserDto userDto){
		return service.update(userDto);
	}

	@DeleteMapping("delete")
	public boolean delete(String id){
		return service.deleteByUserId(id);
	}

	@PostMapping("batch")
	public boolean batch(@RequestBody  UserDto userDto){
		return service.batch(userDto.getType(),userDto.getUserIds());
	}
}
